/**************************************************************************
 * Copyright (C) 2012 Tyler Robertson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **************************************************************************/



/*************************************************************
 * Implements basic functions of the wifly device as seen
 * from the perspective of the master microcontroller.
 * 
 * These functions take care of initializing and communicating
 * with the SC16I740 SPI <--> UART bridge automatically.
 *************************************************************/

#ifndef __WIFLYDEVICE_H__
#define __WIFLYDEVICE_H__

#define Crlf( txt ) txt "\r\n"

/* All slaves should be initialized before the wifly device otherwise 
 * the SC16I740 bridge will hang and will fail to initialize correctly
 */
bool wifly_initialize();

bool wifly_enter_command_mode();
bool wifly_is_in_command_mode();
bool wifly_exit_command_mode();

bool wifly_factory_reset();

bool wifly_join_network( const char *ssid, const char *password );

void wifly_send_byte( unsigned char data );
unsigned char wifly_receive_byte();

void wifly_send_data( const unsigned char *data, unsigned int size_in_bytes );
void wifly_receive_data( unsigned char *data, unsigned int size_in_bytes );

/* Returns true if there is data availale in the receive buffer */
bool wifly_has_data_available();

unsigned char wifly_get_num_bytes_available();

// Should be NULL terminated
void wifly_send_string( const char *str );

bool wifly_send_command( const char *cmd, const char *desiredResponse = 0, unsigned long timeoutMS = 5000 );

bool wifly_open_connection( const char *host, unsigned long port );
bool wifly_close_connection();

bool wifly_is_associated();

void wifly_debug_serial();

#endif/*  __WIFLYDEVICE_H__ */
